﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections.Generic;
public partial class BaseData_COLORPIC : System.Web.UI.Page
{
    public string ggdm = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("200200101");
        if (Request.QueryString["ggdm"] == null)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.opener=null;window.close();", true);
        }
        else
        {
            ggdm = Request.QueryString["ggdm"].ToString().Trim();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (File1.Value.Trim() == "")
        {
            Share.Alert(Page, "请选择要上传的文件！"); return;
        }

        System.Drawing.Image m_Image = System.Drawing.Image.FromStream(File1.PostedFile.InputStream, true);

        string filePath = File1.PostedFile.FileName;
        string type = File1.PostedFile.ContentType;
        string filename = DateTime.Now.Ticks.ToString();
        string filenameExtension = filePath.Substring(filePath.LastIndexOf(".")).ToString().Trim();

        string absolutePath = Server.MapPath("../ColorImageUpload/" + filename + filenameExtension);
        File1.PostedFile.SaveAs(absolutePath);

        if (!File.Exists(absolutePath))
        {
            Share.Alert(Page, "上传文件失败，请重试！"); return;
        }

        Model.SYSPARAM param = new Model.SYSPARAM();
        param = new BLL.SYSPARAM().GetModel("001");
        List<int> guige = new List<int>();
        guige.Add(param.SP_PARAM7);
        guige.Add(param.SP_PARAM8);

        string imgname = ggdm + ".gif";
        string absolutePath1 = Server.MapPath("../ColorImageUpload/" + imgname);

        if (param.SP_PARAM1 == 1)
        {
            string ftpip = param.SP_PARAM13;
            string ftpuser = param.SP_PARAM14;
            string ftppwd = param.SP_PARAM15;
            //Stream st = File1.PostedFile.InputStream;
            Share.Convert(m_Image, HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/ColorImageUpload/" + imgname), guige[0], guige[1]);
            try
            {
                bool result = false;
                FtpUpDown fud = new FtpUpDown(ftpip, ftpuser, ftppwd);
                if (!fud.IsExistsDirectory("", "color", out result))
                {
                    if (result == false)
                    {
                        Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                    }
                    if (!fud.MakeDir("", "color"))
                    {
                        Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                    }
                }
                else
                {
                    if (result == false)
                    {
                        Share.Alert(this.Page, "验证目录是否存在是出现错误,请重试!"); return;
                    }
                }

                if (!fud.Upload("color/", absolutePath1, imgname))
                {
                    if (File.Exists(absolutePath1))
                    {
                        File.Delete(absolutePath1);
                    }
                    Share.Alert(this.Page, "上传失败"); return;
                }
            }
            catch
            {
                Share.Alert(this.Page, "上传失败");
            }
        }
        else
        {
            Share.Convert(m_Image, HttpContext.Current.Server.MapPath(BLL.Common.GetWebPath() + "/Color/" + imgname), guige[0], guige[1]);
        }

        if (new BLL.GUIGE1().InsertColorPic(ggdm, imgname))
        {
            if (File.Exists(absolutePath1))
            {
                File.Delete(absolutePath1);
            }
            Page.ClientScript.RegisterStartupScript(this.GetType(), "close", "window.opener.location.reload();window.opener=null;window.close();", true);
        }
        else
        {
            if (File.Exists(absolutePath1))
            {
                File.Delete(absolutePath1);
            }
            Share.Alert(this.Page, "上传失败");
        }
    }
}
